package cn.tedu.fresheveryday.mapper;

import cn.tedu.fresheveryday.pojo.entity.Product;
import cn.tedu.fresheveryday.pojo.dto.ProductPagerDTO;
import cn.tedu.fresheveryday.pojo.vo.ProductSimpleVO;
import cn.tedu.fresheveryday.pojo.vo.ProductSubOrderDetailVO;
import cn.tedu.fresheveryday.pojo.vo.ProductViewHistoryDetailVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ProductMapper {
    @Select("select name, img_url, price, unit from product where id = #{id}")
    @Result(column = "img_url", property = "imgUrl")
    ProductSubOrderDetailVO selectSubOrderDetailById(int id);

    /**
     * 查询最近上架的3件商品
     * @return
     */
    @Select("select id, name, img_url, price from product order by shelf_time desc limit 0, 3")
    @Result(column = "img_url", property = "imgUrl")
    List<ProductSimpleVO> selectNew();

    /**
     * 查询全部商品
     * @return
     */
    @Select("select id, name, description, detail, img_url, price, unit, quantity from product order by id")
    @Result(column = "img_url", property = "imgUrl")
    List<Product> selectAll();

    /**
     * 通过id查询商品
     * @param id
     * @return
     */
    @Select(
            "select id, name, description, detail, img_url, price, unit, quantity, view_count " +
            "from product where id = #{id}"
    )
    @Result(column = "img_url", property = "imgUrl")
    @Result(column = "shelf_time", property = "shelfTime")
    @Result(column = "view_count", property = "viewCount")
    Product selectById(Integer id);

    /**
     * 通过id查询商品
     * @param id
     * @return
     */
    @Select("select id, name, img_url, price, unit from product where id = #{id}")
    @Result(column = "img_url", property = "imgUrl")
    ProductViewHistoryDetailVO selectDetailById(Integer id);

    /**
     * 通过商品类型id查询商品
     * called by：ProductCategoryMapper.selectAll()
     * @param categoryId 商品类型id
     * @return
     */
    @Select(
            "select id, name, img_url, price from product " +
            "where category_id = #{categoryId} " +
            "order by id limit 0, 4"
    )
    @Result(column = "img_url", property = "imgUrl")
    List<ProductSimpleVO> selectByCid(int categoryId);

    /**
     * 通过id查询商品数量
     * @param id
     * @return
     */
    @Select("select quantity from product where id = #{id}")
    int selectQuantityById(int id);

    /**
     * 根据商品类型id和商品名查询商品
     * @param productPagerDTO
     * @return
     */
    List<Product> selectByPage(ProductPagerDTO productPagerDTO);

    /**
     * 根据商品类型id和商品名查询商品数量
     * @param productPagerDTO
     * @return
     */
    int selectCountByPage(ProductPagerDTO productPagerDTO);

    /**
     * 通过id修改商品数量
     * @param id 商品id
     * @param quantity 商品数量
     */
    @Update("update product set quantity = #{quantity} where id = #{id}")
    void updateQuantityById(@Param("id") int id, @Param("quantity") int quantity);

    /**
     * 商品浏览量增1
     * @param id 商品id
     */
    @Update("update product set view_count = view_count + 1 where id = #{id}")
    void increaseViewCount(int id);
}